﻿using System.Collections;
using WHC.Security.Entity;
using WHC.Security.IDAL;
using System.Collections.Generic;

namespace WHC.Security.BLL
{
    public class Function : BaseBLL<FunctionInfo>
	{
		private IFunction functionDal;

        public Function()
            : base()
        {
            functionDal = baseDal as IFunction;
        }

		private void FillNames(int pID, int currentID, string typeID, int level, ref ArrayList functionNamesAndIDs)
		{
			List<FunctionInfo> list = this.Find(string.Concat(new object[] {"PID=", pID, " And SystemType_ID='", typeID, "'"}));
			int iD = 0;
			foreach (FunctionInfo info in list)
			{
				iD = info.ID;
				if (iD != currentID)
				{
					functionNamesAndIDs.Add(new string('-', level*4) + info.Name);
					functionNamesAndIDs.Add(iD.ToString());
					this.FillNames(iD, currentID, info.SystemType_ID, level + 1, ref functionNamesAndIDs);
				}
			}
		}

        public ArrayList GetCanSetParent(int functionID, string typeID)
		{
			ArrayList functionNamesAndIDs = new ArrayList();
			this.FillNames(-1, functionID, typeID, 0, ref functionNamesAndIDs);
			return functionNamesAndIDs;
		}


		public List<FunctionInfo> GetFunctions(int roleID)
		{
			return this.functionDal.GetFunctions(roleID.ToString(), "");
		}

        public List<FunctionInfo> GetFunctions(string roleIDs, string typeID)
		{
			if (roleIDs == string.Empty)
			{
				roleIDs = "-1";
			}
			return this.functionDal.GetFunctions(roleIDs, typeID);
		}

        public List<FunctionInfo> GetFunctionsByRole(int roleID)
		{
			return this.functionDal.GetFunctionsByRole(roleID);
		}
        public List<FunctionInfo> GetAllFunctions()
        {
            return this.functionDal.GetAllFunctions();
        }
        public List<FunctionInfo> GetAllFirstLevelFunctions()
        {
            return this.functionDal.GetAllFirstLevelFunctions();
        }
        public List<FunctionInfo> GetFunctionsByUser(int userID, string typeID)
		{
			List<RoleInfo> rolesByUser = new Role().GetRolesByUser(userID);
			string roleIDs = ",";
			foreach (RoleInfo info in rolesByUser)
			{
				roleIDs = roleIDs + info.ID + ",";
			}
			List<FunctionInfo> functions = new List<FunctionInfo>();
			if (roleIDs.Length > 1)
			{
				roleIDs = roleIDs.Substring(1, roleIDs.Length - 2);
				functions = this.GetFunctions(roleIDs, typeID);
			}
			return functions;
		}
	}
}